package pteam;

import java.util.*;

import simulator.Actor;
import simulator.Robot;
import simulator.Sensor;

public class ShortPathActor implements Actor{
public static Stack<Decision> decisions;//decisions taken (including choosing to continue straight)

public static final int NORTH = 0;
public static final int WEST = 1;
public static final int SOUTH = 2;
public static final int EAST = 3;

public static int facing = NORTH;

public static final int right = 0;
public static final int straight = 1;//was center but later on you called it 'straight'
public static final int left = 2;

public ShortPathActor (Stack<Decision> d){
decisions = d;

}
public void updateSensors(Sensor[] s) {
    int i = 0;
    for(Sensor sensor : s) {
      double val = sensor.getState()[0];
      if(sensor.getName().equals("RangeFinder")) {
        val = sensor.getState()[i];
        i++;
//  Simulator.out.println("RangeFinder found (val = " + val + ")");
      } else {
//  Simulator.out.println("another type of sensor found and ignored: " + sensor.getName());
      }
    }
    
}


public static Stack<Decision> flipstack(Stack<Decision> d){
// turn decisions over
  Stack<Decision> playstack = new Stack<Decision>();
  Stack<Decision> newstack = new Stack<Decision>();
  while (!playstack.isEmpty()){
    newstack.push(playstack.pop());
  }
  return newstack;
}
  
public void act(Robot r){
  decisions = flipstack(decisions);
  useDecision(r,decisions);
} 
public static void useDecision(Robot r, Stack<Decision> decisions){
//1st take decision out of stack
Decision crD = decisions.pop();
	
// turn direction indicated by decision
	int direction = crD.getDirection();//was getDir-- look in Decision.java 

	// analyze direction in relation to crDirection to determine turn
		int turn;
		if((direction-facing)%4 == 3){
		turn = right;
		}
		else if(direction == facing){
		turn = straight;
		}
		turn = left;
		
	if(turn == right){
	r.turnRight();
	r.waitAngle(90);
	facing = (facing+1)%4;
	}
	else if(turn == left){
	r.turnLeft();
	r.waitAngle(90);
	facing = ((facing - 1) % 4);
	}
	//else keep going straight

// move forward
r.goForward();
r.waitDistance(40);
}

public String getMaze(){
return "example";
}




}